<script type="text/javascript">
$(document).ready(function(){
	$("#listSet table tbody tr:even").css("background", "#fff");
	$("#listSet table tbody tr:odd").css("background", "#fee");
	$("#setForm").validate();
	$("#setForm").ajaxForm({
		beforeSubmit: function(){
			if (!$("#setForm").valid())
				return false;
			var len = $(".ids").length;
			var aa =[];
			$.each($(".ids"),function(i,val){
					if ($.inArray($(this).val(),aa) >-1){
						return false;
					}
					aa[i] = $(this).val();
			});
			if (len >aa.length) {
				alert('<?php echo $this->translate('id repeat')?>');
				return false;
			}
		},
		success: function(response){
			if (response) {
				alert('<?php echo $this->translate('success')?>');
			}
		}
		});

		$(".itemDelete").click(function(){
			if (confirm('<?php echo $this->translate('Sure you want to delete?')?>')){
				var sn = $(this).attr('id').substr(2);
				var aa = $(this);
				$.get('<?php echo $this->route('delete')?>',{sn:sn},function(){
					$(aa).parent().parent().remove();
				});
			}
		});

		$("#appendBtn").click(function(){
			var html = $("#dialog tbody tr").clone().addClass('append_tr');
			var id = $(".append_tr").length;
			$(html).children().each(function(){
				$(this).children().attr('name',$(this).children().attr('name')+'['+id+']');
			});

			$("#setTable tbody:first").prepend(html);


		});

		$(".cancelBtn").live('click',function(){
			$(this).parents('tr').remove();
			});


	});
</script>
<style>
<!--
.append_tr {background:#ff8}
-->
</style>

<div id="listSet">
<form action="<?php echo $this->route('setall')?>" method="post"  id="setForm">
<table>
<tr><td><h2><?php echo  $this->name?></h2></td>
<td><input  type="submit" id="act"  value="<?php echo $this->translate('save')?>"  /></td>
<td><input  type="button" id="appendBtn"  value="<?php echo $this->translate('append')?>"  /></td>
</tr>
</table>
<table  id="setTable">
<thead>
<tr>
<th><?php echo $this->translate('sort')?></th>
<th><?php echo $this->translate('id')?></th>
<th><?php echo $this->translate('name')?></th>
<th><?php echo $this->translate('action')?></th>
</tr>
</thead>
<tbody>
<?php foreach($this->data as $val):?>
<tr>
<td><input type="text" name="sort[<?php echo $val['sn']?>]" value="<?php echo $val['sort']?>" size=3  class="required"/></td>
<td><input class="ids required" type="text" name="id[<?php echo $val['sn']?>]" value="<?php echo $val['id']?>" size=8 /></td>
<td><input type="text" name="name[<?php echo $val['sn']?>]" value="<?php echo $val['name']?>" size="20" class="required" /></td>
<td><a id="d-<?php echo $val['sn']?>" href='#' class='itemDelete'><?php echo $this->translate('delete')?></a></td>
</tr>
<?php endforeach;?>
</tbody>
</table>
<input type="hidden"  id="parent_sn" name="parent_sn" value="<?php echo $this->parent_sn?>" />
<input type="hidden"  id="kind" name="kind" value="<?php echo $this->kind?>" />
</form>
</div>
<div id="dialog" style="display:none" title="<?php echo $this->translate('append')?>">
<table>
<tbody>
<tr>
<td><input type="text" name="sorts" size="3"  class="required"/></td>
<td><input type="text" name="ids" size="8"  class="required"/></td>
<td><input type="text" name="names" size="20"  class="required"/></td>
<td><input type="button"  class="cancelBtn"  value="<?php echo $this->translate('cancel')?>" /></td>
</tr>
</tbody>
</table>

</div>
